import global from './global.js';

let ad = null;
// 激励视频广告
let adVideoIdDouyin = global.curAppInfo.douyin.adVideoId;
let adVideoIdKuaishou = global.curAppInfo.kuaishou.adVideoId;
let adVideoIdBaidu = global.curAppInfo.baidu.adVideo;
let appVideoIdBaidu = global.curAppInfo.baidu.appVideoId;
// banner广告
let adBannerIdDouyin = global.curAppInfo.douyin.adBannerId;

// // 启动广告
// let adUnitIdDouyinStart = global.curAppInfo.adUnitIdDouyinStart;
// // 首页底部广告
// let adUnitIdDouyinHomeBottom = global.curAppInfo.adUnitIdDouyinHomeBottom;
// let adUnitIdKuaishouHomeBottom = global.curAppInfo.adUnitIdKuaishouHomeBottom;
// // 结果页底部广告
// let adUnitIdDouyinResultBottom = global.curAppInfo.adUnitIdDouyinResultBottom;
// let adUnitIdKuaishouResultBottom = global.curAppInfo.adUnitIdKuaishouResultBottom;
// // 个人页底部广告
// let adUnitIdDouyinPersonBottom = global.curAppInfo.adUnitIdDouyinPersonBottom;
// let adUnitIdKuaishouPersonBottom = global.curAppInfo.adUnitIdKuaishouPersonBottom;

let provider = global.getProvider();

// 创建视频广告
function adVideoCreate() {

}

// 展示视频广告
function adVideoShow(callBack) {
	switch (provider) {
		case "douyin": {
			ad = tt.createRewardedVideoAd({
				adUnitId: adVideoIdDouyin,
			});
			ad.load();
			ad.onClose((data) => {
				console.log("onClose", data);
				tt.hideLoading();
				if (data.isEnded) {
					callBack();
				} else {
					global.toast("观看完视频才能继续喔~");
				}
			});
			ad.onError((err) => {
				console.log("onError", err);
				tt.hideLoading();
				switch (err.errCode) {
					default: {
						callBack();
					}
				}
			});
			ad.show();
			break;
		}
		case "toutiao": {
			ad = tt.createRewardedVideoAd({
				adUnitId: adVideoIdDouyin,
			});
			ad.load();
			ad.onClose((data) => {
				console.log("onClose", data);
				tt.hideLoading();
				if (data.isEnded) {
					callBack();
				} else {
					global.toast("观看完视频才能继续喔~");
				}
			});
			ad.onError((err) => {
				console.log("onError", err);
				tt.hideLoading();
				switch (err.errCode) {
					default: {
						callBack();
					}
				}
			});
			ad.show();
			break;
		}
		case "kuaishou": {
			ad = ks.createRewardedVideoAd({
				type: 100011056,
				unitId: adVideoIdKuaishou,
			})
			ad.onClose((data) => {
				console.log("onClose", data);
				if (data.isEnded) {
					callBack();
				} else {
					global.toast("观看完视频才能继续喔~");
				}
			});
			ad.onError((err) => {
				console.log("onError", err);
				switch (err.errCode) {
					default: {
						callBack();
					}
				}
			});
			ad.show();
			break;
		}
		case "baidu": {
			console.log(ad);
			if (swan.createRewardedVideoAd) {
				ad = swan.createRewardedVideoAd({
					adUnitId: adVideoIdBaidu,
					appSid: appVideoIdBaidu
				})
			}
			if (ad) {
				ad.onClose((data) => {
					console.log("onClose", data);
					if (data.isEnded) {
						callBack();
					} else {
						global.toast("观看完视频才能继续喔~");
					}
				});
				ad.onError((err) => {
					console.log("err", err);
					switch (err.errCode) {
						default: {
							callBack();
						}
					}
				});
				ad.show().catch((res1) => {
					console.log("res1", res1);
					ad.load().then(res2 => {
						console.log("res2", res2);
						ad.show();
					})
				})
			} else {
				callBack();
			}
			break;
		}
		default: {
			callBack();
		}
	}
}

// 关闭视频广告
function adVideoUnload() {
	switch (provider) {
		case "baidu": {
			if (ad) {
				ad.offClose();
			}
			break;
		}
		default: {
			ad = null;
		}
	}
}

// 广告确认弹窗
function showAdConfirmModal(callBack) {
	uni.showModal({
		title: "提示",
		content: "首次解锁需要观看广告",
		showCancel: true,
		success: (res) => {
			if (res.confirm) {
				callBack();
			}
		},
	});
}

module.exports = {
	adVideoCreate,
	adVideoShow,
	adVideoUnload,
	showAdConfirmModal,
	adBannerIdDouyin,
}